package com.nd.rj.common.thirdPlatformAuth.operateImpl;

import android.app.Activity;
import android.app.Dialog;

import com.nd.rj.common.ILogin99Listener;
import com.nd.rj.common.thirdPlatformAuth.ThirdPlatformAuthConst.THIRD_PLATFORM_TYPE;
import com.nd.rj.common.thirdPlatformAuth.IOperate.IThirdPlatformAuthBase;
import com.nd.rj.common.thirdPlatformAuth.IOperate.IThirdPlatformAuthOperate;
import com.product.android.business.bean.UserInfo;

/**
 * ClassName:NdLogin99PlatformImpl Function: 登录到91帐号中心相关流程 Reason:
 * 在V1.1之后要采用更加严谨的登录方式以防止恶意攻击，原来的json串形式登录到UAP要改成字节流的形式先登录到
 * 91帐号中心，获取到cookie之后再用这个cookie再登录到UAP或者OAP。 Attention: 1、此类用于应用自已创建登录的界面而不使用共通登录模块里的登录界面
 * 2、在自定义的登录界面的onCreate方法务必要先获取此方法的一个单体实例，以便初始化登录环境
 * 3、必须在自定义的界面里实现NdMiscCallbackListener.IGetCheckcodeListener接口回调，用于 　
 * 显示验证码。因为在此登录模式下，密码错误若干次（现在是4次）后必须输入验证码才能登录 4、具体调用方式可参考NdLogin.java类
 * 
 * @author cb
 * @version
 * @since Ver 1.1
 * @Date 2013 2013-5-27 下午3:27:49
 * 
 * @see
 */
public class ThirdPlatformAuthImpl implements IThirdPlatformAuthOperate{
    /** TAG */
    private static final String TAG = ThirdPlatformAuthImpl.class.getSimpleName();
    
    /**页面*/
    private Activity mActivity;
    
    private UserInfo mUser;
    
    /**登录结果监听*/
    private ILogin99Listener mLogin99Listener;
    
    /**处理时的等待对话框*/
    private Dialog mWaitDlg;
    
    /**当前登录的平台类型*/
    private int miCurrentPlatformType = -1;
    
    /**新浪微博认证接口*/
    private IThirdPlatformAuthBase mIQQAuth;
    
    /**QQ认证接口*/
    private IThirdPlatformAuthBase mISinaWeiboAuth;
    
    
    /**
     * 构造方法
     * Creates a new instance of NdLogin99PlatformImpl.
     * <br>Created 2014-9-22 下午9:13:48
     * @param context   上下文
     * @param bInit     是否要初始化
     */
    public ThirdPlatformAuthImpl(Activity activity){
        mActivity = activity;
    }
    
    @Override
    public void setThirdPlatformType(int iPlatformType) {
        miCurrentPlatformType = iPlatformType;
    }
    
    @Override
    public void setLogin99WaitDlg(Dialog waitDlg) {
        mWaitDlg = waitDlg;
    }
    
    @Override
    public void setLogin99Listener(ILogin99Listener listener) {
        mLogin99Listener = listener;
    }

    @Override
    public void doThirdPlatformAuth() {
        IThirdPlatformAuthBase IBaseInterface = getCurrentPlatformInterface();
        if(null != IBaseInterface){
            IBaseInterface.doAuth();
        }
    }

    @Override
    public void doThirdPlatformLogout() {
        IThirdPlatformAuthBase IBaseInterface = getCurrentPlatformInterface();
        if(null != IBaseInterface){
            IBaseInterface.doLogout();
        }
    }
    
    /**
     * 根据当前的平台类型，得到对应的平台登录认证接口
     * 
     * <br>Created 2014-11-5 下午4:44:30
     * @return
     * @author       linsj
     */
    private IThirdPlatformAuthBase getCurrentPlatformInterface(){
        switch (miCurrentPlatformType) {
        case THIRD_PLATFORM_TYPE.THIRD_PLATFORM_SINA_WEIBO:
            return getSinaWeiboAuthInterface();
        case THIRD_PLATFORM_TYPE.THIRD_PLATFORM_QQ:
            return getQQAuthInterface();
        default:
            return null;
        }
    }

    /**
     * 得到QQ登录认证接口成员变量
     * 
     * <br>Created 2014-11-5 下午4:43:20
     * @author       linsj
     */
    private IThirdPlatformAuthBase getQQAuthInterface(){
        if(null == mIQQAuth){
            mIQQAuth = new QQLoginAuth(mActivity);
        }
        
        return mIQQAuth;
    }
    
    /**
     * 得到新浪微博登录认证接口成员变量
     * 
     * <br>Created 2014-11-5 下午4:43:20
     * @author       linsj
     */
    private IThirdPlatformAuthBase getSinaWeiboAuthInterface(){
        if(null == mISinaWeiboAuth){
            mISinaWeiboAuth = new SinaWeiboLoginAuth(mActivity);
        }
        
        return mISinaWeiboAuth;
    }
}
